package hd3.examples;

import hdapi3.HD3;
import hdapi3.Settings;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.logging.Logger;

public class AllInOneTest {
	private final static Logger g_logger = Logger.getLogger(AllInOneTest.class.toString());
	private HD3 hd3;
	public AllInOneTest() {
		
	}
	public void testInit() {
		g_logger.info("testing init");
		try {
			FileInputStream fis = new FileInputStream("hdapi_config.properties");
			Settings.init(fis);
			fis.close();
			hd3 = new HD3();
		} catch (Exception e) {
			g_logger.severe(e.getMessage());
			e.printStackTrace();
		}
	}
	
	public void testSiteFetchTrees() {
		g_logger.info("testing fetch trees.");
		if (hd3.siteFetchTrees()) {
			g_logger.info("trees fetched.");
		} else {
			g_logger.severe(hd3.getError());
		}
	}
	
	public void testSiteFetchSpecs() {
		g_logger.info("testing fetch specs.");
		if (hd3.siteFetchSpecs()) {
			g_logger.info("specs fetched.");
		} else {
			g_logger.severe(hd3.getError());
		}
	}
	
	public void testRemoteDeviceVendors() {
		g_logger.info("testing remote device vendors.");
		hd3.setUseLocal(false);
		if (hd3.deviceVendors()) {
			g_logger.info(hd3.getReply().toString());
		} else {
			g_logger.severe(hd3.getError());
		}
	}
	
	public void testLocalDeviceVendors() {
		g_logger.info("testing local device vendors.");
		hd3.setUseLocal(true);
		if (hd3.deviceVendors()) {
			g_logger.info(hd3.getReply().toString());
		} else {
			g_logger.severe(hd3.getError());
		}
	}
	
	public void testRemoteDeviceModels() {
		g_logger.info("testing remote device models.");
		hd3.setUseLocal(false);
		if (hd3.deviceModels("Nokia")) {
			g_logger.info(hd3.getReply().toString());
		} else {
			g_logger.severe(hd3.getError());
		}
	}
	
	public void testLocalDeviceModels() {
		g_logger.info("testing local device models.");
		hd3.setUseLocal(true);
		if (hd3.deviceModels("Nokia")) {
			g_logger.info(hd3.getReply().toString());
		} else {
			g_logger.severe(hd3.getError());
		}
	}
	
	public void testRemoteDeviceView() {
		g_logger.info("testing remote device view.");
		hd3.setUseLocal(false);
		if (hd3.deviceView("Nokia", "660")) {
			g_logger.info(hd3.getReply().toString());
		} else {
			g_logger.severe(hd3.getError());
		}
	}
	
	public void testLocalDeviceView() {
		g_logger.info("testing local device view.");
		hd3.setUseLocal(true);
		if (hd3.deviceView("Nokia", "660")) {
			g_logger.info(hd3.getReply().toString());
		} else {
			g_logger.severe(hd3.getError());
		}
	}
	
	public void testRemoteDeviceWhatHas() {
		g_logger.info("testing remote device whathas.");
		hd3.setUseLocal(false);
	    if (hd3.deviceWhatHas("general_vendor", "Nokia")) {
			g_logger.info(hd3.getReply().toString());
		} else {
			g_logger.severe(hd3.getError());
		}
	}
	
	public void testLocalDeviceWhatHas() {
		g_logger.info("testing local device whathas.");
		hd3.setUseLocal(true);
	    if (hd3.deviceWhatHas("general_vendor", "Nokia")) {
			g_logger.info(hd3.getReply().toString());
		} else {
			g_logger.severe(hd3.getError());
		}
	}
	
	public void testRemoteDetect1() {
		g_logger.info("testing remote detect case 1.");
		hd3.setUseLocal(false);
		hd3.resetDetectRequest();
		hd3.addDetectVar(
				"user-agent",
				"Mozilla/5.0 (SymbianOS/9.2; U; Series60/3.1 NokiaN95/12.0.013; Profile/MIDP-2.0 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413");
		if (hd3.siteDetect()) {
			g_logger.info(hd3.getReply().toString());
		} else {
			g_logger.severe(hd3.getError());
		}
	}
	
	public void testLocalDetect1() {
		g_logger.info("testing local detect case 1.");
		hd3.setUseLocal(true);
		hd3.resetDetectRequest();
		hd3.addDetectVar(
				"user-agent",
				"Mozilla/5.0 (SymbianOS/9.2; U; Series60/3.1 NokiaN95/12.0.013; Profile/MIDP-2.0 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413");
		if (hd3.siteDetect()) {
			g_logger.info(hd3.getReply().toString());
		} else {
			g_logger.severe(hd3.getError());
		}
	}
	
	public void testRemoteDetect2() {
		g_logger.info("testing remote detect case 2.");
		hd3.setUseLocal(false);
	    HashMap<String, String> headers = new HashMap<String, String>();
	    headers.put("user-agent", "Opera/9.80 (Android; OperaMini/7.0.29952/28.2144; U; pt) Presto/2.8.119 Version/11.10");
		headers.put("x-operamini-phone", "Android #");
		headers.put("x-operamini-phone-ua", "Mozilla/5.0 (Linux; U;Android 2.1-update1; pt-br; U20a Build/2.1.1.A.0.6) AppleWebKit/530.17(KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
		hd3.setup(headers, null, null);
		if (hd3.siteDetect()) {
			g_logger.info(hd3.getReply().toString());
		} else {
			g_logger.severe(hd3.getError());
		}
	}
	
	public void testLocalDetect2() {
		g_logger.info("testing local detect case 2.");
		hd3.setUseLocal(false);
	    HashMap<String, String> headers = new HashMap<String, String>();
	    headers.put("user-agent", "Opera/9.80 (Android; OperaMini/7.0.29952/28.2144; U; pt) Presto/2.8.119 Version/11.10");
		headers.put("x-operamini-phone", "Android #");
		headers.put("x-operamini-phone-ua", "Mozilla/5.0 (Linux; U;Android 2.1-update1; pt-br; U20a Build/2.1.1.A.0.6) AppleWebKit/530.17(KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
		hd3.setup(headers, null, null);
		if (hd3.siteDetect()) {
			g_logger.info(hd3.getReply().toString());
		} else {
			g_logger.severe(hd3.getError());
		}
	}
	
	public void testRemoteDetect3() {
		g_logger.info("testing remote detect case 3.");
		hd3.setUseLocal(false);
	    HashMap<String, String> headers = new HashMap<String, String>();
	    headers.put("user-agent", "Opera/9.80 (J2ME/MIDP; OperaMini/4.2.14004/28.2144; U; en) Presto/2.8.119 Version/11.10");
		headers.put("x-operamini-phone", "Alcatel # OT 800");
		headers.put("x-operamini-phone-ua", "Alcatel-OT-800A");
		hd3.setup(headers, null, null);
		if (hd3.siteDetect()) {
			g_logger.info(hd3.getReply().toString());
		} else {
			g_logger.severe(hd3.getError());
		}
	}
	
	public void testLocalDetect3() {
		g_logger.info("testing remote detect case 3.");
		hd3.setUseLocal(true);
		HashMap<String, String> headers = new HashMap<String, String>();
	    headers.put("user-agent", "Opera/9.80 (J2ME/MIDP; OperaMini/4.2.14004/28.2144; U; en) Presto/2.8.119 Version/11.10");
		headers.put("x-operamini-phone", "Alcatel # OT 800");
		headers.put("x-operamini-phone-ua", "Alcatel-OT-800A");
		hd3.setup(headers, null, null);
		if (hd3.siteDetect()) {
			g_logger.info(hd3.getReply().toString());
		} else {
			g_logger.severe(hd3.getError());
		}
	}
	
	public void testRemoteDetect4() {
		g_logger.info("testing remote detect case 4.");
		hd3.setUseLocal(false);
	    HashMap<String, String> headers = new HashMap<String, String>();
	    headers.put("user-agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; mowser; http://www.mowser.com)");
		headers.put("x-mobile-ua", "Opera/9.80 (J2ME/MIDP; Opera Mini/4.4.29476/28.2144;U; en) Presto/2.8.119 Version/11.10");
		hd3.setup(headers, null, null);
		if (hd3.siteDetect()) {
			g_logger.info(hd3.getReply().toString());
		} else {
			g_logger.severe(hd3.getError());
		}
	}
	
	public static void main(String[] args) {
		try {
			AllInOneTest example = new AllInOneTest();
			example.testInit();
			example.testSiteFetchTrees();
			example.testSiteFetchSpecs();
			example.testRemoteDeviceVendors();
			example.testLocalDeviceVendors();
			example.testRemoteDeviceModels();
			example.testLocalDeviceModels();
			example.testRemoteDeviceView();
			example.testRemoteDeviceWhatHas();
			example.testLocalDeviceWhatHas();
			example.testRemoteDetect1();
			example.testLocalDetect1();
			example.testLocalDetect2();
			example.testRemoteDetect2();
			example.testRemoteDetect3();
			example.testLocalDetect3();
			example.testRemoteDetect4();
		} catch (Exception ie) {
			ie.printStackTrace();
			g_logger.severe(ie.getMessage());
		}
	}
}
